package com.csw.demojson.controller.jackson;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.type.CollectionType;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class JacksonDemo {
	public static void main(String[] args) throws JsonProcessingException {
		// 总结 ObjectMapper mapper = new ObjectMapper();
		// map对象,list对象,或实体对象转JSONString //mapper.writeValueAsString(对象);
		// JSONString转map //mapper.readValue(jsonString, Map.class);
		// JSONString转实体对象 //mapper.readValue(jsonString, 对象.class);
		// JSONString转list<实体对象>
		// CollectionType listType =
		// mapper.getTypeFactory().constructCollectionType(ArrayList.class, 对象.class);
		// List<对象> listJson = mapper.readValue(jsonString, listType);
		//// 当里面是Object或其他类型的时候，对象换成object
		// JSONArray和JSONObject先转换成JSONString，再按照上面方法转，记住JSONString是中转


		// 转json字符串
		String json = new ObjectMapper().writeValueAsString(new Object());


		// json字符串转Map
		Map mapJson = new ObjectMapper().readValue(json, Map.class);


		// json字符串转对象数组List
		CollectionType listType = new ObjectMapper().getTypeFactory().constructCollectionType(ArrayList.class, Object.class);
		List<Object> listJson = new ObjectMapper().readValue(json, listType);


		// Json字符串转对象
		Object o = new ObjectMapper().readValue(json, Object.class);

	}

}
